let pressValues = [0.3, 0.5]; // 初始两个 press 值
let chartData = []; // 存储图表数据

function updateChartData(pressValue, xAxisData, yAxisData) {
    let updated = false;
    for (let i = 0; i < yAxisData.length; i++) {
        if (yAxisData[i] <= pressValue && !chartData.some(data => data.y === yAxisData[i])) {
            chartData.push({ x: xAxisData[i], y: yAxisData[i], symbol: 'pin' });
            updated = true;
        } else if (yAxisData[i] > pressValue) {
            break; // 结束更新
        }
    }
    return updated;
}

//获取echarts
export const updataChart = (pressValue, xAxisData, yAxisData, lineChart) => {
    pressValues.push(pressValue);
    const lastPressValue = pressValues[pressValues.length - 1];
    const updated = updateChartData(lastPressValue, xAxisData, yAxisData);
    if (updated) {
        const options = {
            grid: {
                left: '15%',
            },
            xAxis: {
                type: 'value',
                name: '容积变化量（cm³）',
                nameGap: 50,
                nameLocation: 'center',
                axisLabel: {
                    rotate: 45
                }
            },
            yAxis: {
                type: 'value',
                name: 'LOAD（MPa）',
                nameGap: 30,
                nameLocation: 'center'
            },
            tooltip: {
                trigger: "axis"
            },
            series: [
                {
                    type: 'line',
                    smooth: true,
                    data: chartData.map(data => [data.x.toFixed(6), data.y.toFixed(3)]),
                }
            ]
        };
        lineChart.setOption(options);
        return lineChart;
    }
}
